# 只是下落雨滴
import sys  
import pygame  
import random  
from pathlib import Path   

import variable

# 初始化 Pygame  
pygame.init()  
  
# 设置屏幕大小  
size = width, height = 800, 600  
screen = pygame.display.set_mode(size)  
  
# 设置颜色  
WHITE = (255, 255, 255)  
BLACK = (0, 0, 0)  
  
# 加载图像并获取其 Rect  
ball = pygame.image.load(f'{Path.cwd()}{variable.lufei}')  # 假设你有一个名为 'ball.png' 的图像文件  
ballrect = ball.get_rect(centerx=width // 2, centery=height - ball.get_height() // 2)  
  
# 雨滴类  
class Raindrop:  
    def __init__(self):  
        self.x = random.randint(0, width)  # 随机x坐标  
        self.y = 0 - random.randint(0, 30)  # 从屏幕顶部上方开始  
        self.speed = random.randint(2, 5)  # 随机下落速度  
  
    def move(self):  
        self.y += self.speed  
  
    def draw(self, screen):  
        pygame.draw.circle(screen, (0, 0, 255), (int(self.x), int(self.y)), 2)  
  
    def is_offscreen(self):  
        return self.y > height  
  
# 设置移动速度  
speed = [5, 5]  
  
# 雨滴列表  
raindrops = [Raindrop() for _ in range(10)]  # 初始雨滴数量  
  
# 游戏主循环  
running = True  
clock = pygame.time.Clock()  # 创建一个Clock对象来管理帧率  
while running:  
    for event in pygame.event.get():  
        if event.type == pygame.QUIT:  
            running = False  
  
    # 使用 pygame.key.get_pressed() 来检测按键  
    keys = pygame.key.get_pressed()  
    if keys[pygame.K_LEFT] and ballrect.left > 0:  
        ballrect.move_ip(-speed[0], 0)  
    if keys[pygame.K_RIGHT] and ballrect.right < width:  
        ballrect.move_ip(speed[0], 0)  
  
    # 填充屏幕颜色  
    screen.fill(WHITE)  
  
    # 绘制雨滴  
    for drop in raindrops:  
        drop.move()  
        if not drop.is_offscreen():  
            drop.draw(screen)  
        else:  
            # 当雨滴移出屏幕时，重新生成一个新的雨滴  
            raindrops.remove(drop)  
            raindrops.append(Raindrop())  
  
    # 绘制球  
    screen.blit(ball, ballrect)  
  
    # 更新屏幕显示  
    pygame.display.flip()  
  
    # 控制帧率  
    clock.tick(60)  
  
# 退出 Pygame  
pygame.quit()  
sys.exit()